<?php
class CategoryAction extends Action {

    public function _initialize() {
        $Config = A('Config');
        $Config->checkLogIn();
        $Config->checkUserLevel(array('1', '2'));
    }
    
    public function index() {
        $Category = M('Category');
        import("ORG.Util.Page");
        $count = $Category->count();
        $Page = new Page($count, 25);
        $show = $Page->show();
        $this->assign('page',$show);
        
        $category = $Category->order(array('list_order' => 'asc', 'cat_id' => 'asc'))->limit($Page->firstRow . ',' . $Page->listRows)->select();
        
        $this->assign('category', $category);
        $this->display();
    }
    
    public function add() {
        $Category = M('Category');
        $category = $Category->order(array('list_order' => 'asc'))->select();
        
        $this->assign('category', $category);
        $this->display();
    }
    
    public function insert() {
        if (isset($_POST['submitted'])) {
            $Category = D('Category');
            if ($Category->create()) {
                if ($Category->add()) {
                    $this->success('成功添加分类！');
                } else {
                    $this->error('添加分类失败！');
                }
            } else {
                $this->error($Category->getError());
            }
        }
    }
    
    public function edit() {
        if (isset($_GET['id'])) {
            $id = (int) $_GET['id'];
            $Category = M('Category');
            $category = $Category->find($id);
            
            $cat_list = $Category->order(array('list_order' => 'asc'))->select();
            
            $this->assign('cat_list', $cat_list);
            $this->assign('category', $category);
            $this->display();
        } else {
            $this->assign('jumpUrl', __APP__);
            $this->error('非法操作！');
        }
    }
    
    public function update() {
        if (isset($_POST['submitted'])) {
            $Category = D('Category');
            
            if ($Category->create()) {
                if (false !== $Category->save()) {
                    $this->success('成功编辑分类！');
                } else {
                    $this->error('编辑分类失败！');
                }
            } else {
                $this->error($Category->getError());
            }
        }
    }
    
    public function delete() {
        if (isset($_GET['id'])) {
            $id = (int) $_GET['id'];
            $Category = M('Category');
            
            $File = M('file');
            $count = $File->where(array('cat_id' => $id))->count();
            
            /*
            if ($count > 0) {
                $this->error('请删除该分类下所有内容再删除该分类！');
            } */
            
            if ($Category->delete($id)) {
                $this->success('成功删除分类！');
            } else {
                $this->error('删除分类失败！');
            }
        }
    }
    
    public function updateListOrder() {
        if (isset($_POST['submitted'])) {
            $Category = M('Category');
            $list_order = $_POST['list_order'];
            foreach ($list_order as $key => $value) {
                $Category->cat_id = $key;
                $Category->list_order = $value;
                $Category->save();
            }
            $this->success('成功更新排序！');
        } else {
            $this->error('非法操作！');
        }
    }
    
}
?>